<template>
  <el-container>
    <el-header>
      <div class="report_header" ref="headerHeight">
        <div class="report_header_left"></div>
        <div style="display: flex;flex-direction: row;justify-content:space-between;width: 100%;">
          <div style="display: flex;flex-direction: row;height:41px;justify-content: flex-end;flex: 1">
            <!--            <div class="report_header_query">-->
            <!--              <el-select v-model="query.type" clearable placeholder="标签类型">-->
            <!--                <el-option-->
            <!--                  v-for="item in typeOptions"-->
            <!--                  :key="item.value"-->
            <!--                  :label="item.label"-->
            <!--                  :value="item.value">-->
            <!--                </el-option>-->
            <!--              </el-select>-->
            <!--            </div>-->
            <div class="report_header_query">
              <el-input prefix-icon="el-icon-search" placeholder="标签名" v-model="query.name"></el-input>
            </div>
          </div>
          <div>
            <el-button style="margin-left: 10px;margin-top: 5px;" size="small" type="primary" @click="queryTag">查询
            </el-button>
            <el-button style="margin-left: 10px;margin-top: 5px;" size="small" type="primary" @click="addClick"
                       v-permission="['tag.add']">新增
            </el-button>
          </div>
        </div>
      </div>
    </el-header>
    <div class="pd10" style="padding-top: 0px">
      <el-tabs v-model="activeName" @tab-click="handleClick">
        <el-tab-pane label="导航" name="9"></el-tab-pane>
        <el-tab-pane label="商品" name="3"></el-tab-pane>
        <el-tab-pane label="广告" name="8"></el-tab-pane>
        <el-tab-pane label="文章" name="0"></el-tab-pane>
      </el-tabs>
      <el-table
        :data="tagForm"
        border
        stripe
        v-loadMore="[payLoads,'tag-list']"
        :height="curHeight"
        style="width: 100%">
        <el-table-column
          type="index"
          width="50"
          align="center"
        >
        </el-table-column>
        <el-table-column
          label="标签名称"
          width="180"
          align="center"
          prop="name"
        >
        </el-table-column>
        <el-table-column
          label="备注"
          align="left"
          prop="memo"
        >
        </el-table-column>
        <el-table-column
          label="排序"
          align="center"
          width="80"
        >
          <template slot-scope="scope">
            <el-input v-model="scope.row.orders" @blur="bulrOrders(scope.row)"></el-input>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="80">
          <template slot-scope="scope">
            <div>
              <el-tooltip v-permission="['tag.edit']" class="item" effect="dark" content="编辑" placement="top-start">
                <a @click="editClick(scope.row)">
                  <i style="color: #409EFF" class="el-icon-edit"></i>
                </a>
              </el-tooltip>
              <el-tooltip v-permission="['tag.del']" class="item" effect="dark" content="删除" placement="top-start">
                <a style="margin-left: 10px" @click="deleteTag(scope.row)">
                  <i style="color: #F56C6C" class="el-icon-delete"></i>
                </a>
              </el-tooltip>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <!--新增标签-->
      <el-dialog title="新增标签" v-el-drag-dialog :visible.sync="dialogFormVisible">
        <el-container>
          <el-main class="popup_content">
            <el-form ref="addForm" :model="addForm" :rules="rules" label-width="80px" size="mini">
              <el-form-item label="标签类型" prop="type">
                <el-select style="margin-left: 5px" v-model="addForm.type" placeholder="请选择状态">
                  <el-option label="导航" value="9"></el-option>
                  <el-option label="商品" value="3"></el-option>
                  <el-option label="广告" value="8"></el-option>
                  <el-option label="文章" value="0"></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="标签名称" prop="name">
                <el-input v-model="addForm.name"></el-input>
              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model="addForm.memo"></el-input>
              </el-form-item>
              <el-form-item label="图标">
                <single-upload :singleUploadTitle='singleUploadTitle' v-model="addForm.icon"
                               style="display: inline-block;"></single-upload>
              </el-form-item>
            </el-form>
          </el-main>
        </el-container>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" :loading="clicked" @click="saveTag('addForm')">确 定</el-button>
        </div>
      </el-dialog>
      <!--编辑标签-->
      <el-dialog title="编辑标签" v-el-drag-dialog :visible.sync="editVisible">
        <el-container>
          <el-main class="popup_content">
            <el-form ref="editForm" :model="editForm" :rules="rules" label-width="80px" size="mini">
              <el-form-item label="标签类型" prop="type">
                <el-select style="margin-left: 5px" v-model="editForm.type" placeholder="请选择状态">
                  <el-option label="导航" value="9"></el-option>
                  <el-option label="商品" value="3"></el-option>
                  <el-option label="广告" value="8"></el-option>
                  <el-option label="文章" value="0"></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="标签名称" prop="name">
                <el-input v-model="editForm.name"></el-input>
              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model="editForm.memo"></el-input>
              </el-form-item>
              <el-form-item label="图标">
                <single-upload :singleUploadTitle='singleUploadTitle' :key="Math.random()" v-model="editForm.icon"
                               style="display: inline-block;"></single-upload>
              </el-form-item>
            </el-form>
          </el-main>
        </el-container>
        <div slot="footer" class="dialog-footer">
          <el-button @click="editVisible = false">取 消</el-button>
          <el-button type="primary" :loading="clicked" @click="updateTag('editForm')">确 定</el-button>
        </div>
      </el-dialog>
    </div>
    <el-footer class="el-footerBox">
      <pagination v-show="total>0" :total="total" :page.sync="pageNum" :limit.sync="pageSize"
                  @pagination="loadTag"></pagination>
    </el-footer>
  </el-container>
</template>

<script>
import loadMore from '@/utils/loadMore.js'
import permission from '@/directive/permission/index.js' // 权限判断指令
import {list, del, find, save, update} from '@/api/tag'
import utils from '../../utils/utils.js'
import Pagination from '@/components/Pagination';
import SingleUpload from '@/components/Upload/singleUpload'
import elDragDialog from '@/directive/el-drag-dialog' // base on element-ui
export default {
  directives: {elDragDialog, permission, loadMore},
  activated() {
    this.payLoads = new Date().getTime();
    console.log('1111')
    console.log(utils.getUUID())
    console.log('2222')
  },
  name: 'tag-list',
  data() {
    return {
      singleUploadTitle: '200*200',
      payLoads: '',
      addForm: {
        name: '',
        icon: '',
        memo: '',
        type: '',
      },
      dialogFormVisible: false,
      rules: {
        name: [
          {required: true, message: '请输入标签名称', trigger: 'blur'},
          {min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur'}
        ],
        type: [
          {required: true, message: '请选择标签类型', trigger: 'blur'},
        ]
      },
      editVisible: false,
      editForm: {
        id: '',
        name: '',
        icon: '',
        memo: '',
        type: '',
      },
      tagForm: [],
      pageNum: 1,
      pageSize: 20,
      total: 0,
      queryType: true,
      query: {
        name: '',
        type: 9
      },
      keyword: {
        name: '',
        type: 9
      },
      clicked: false,
      curHeight: null,
      typeOptions: [
        {
          value: 0,
          label: '文章'
        },
        {
          value: 1,
          label: '客户'
        },
        {
          value: 2,
          label: '专题'
        },
        {
          value: 3,
          label: '商品'
        },
        {
          value: 4,
          label: '课程'
        },
        {
          value: 5,
          label: '量表'
        },
        {
          value: 6,
          label: '答主'
        },
        {
          value: 7,
          label: '咨询'
        },
        {
          value: 8,
          label: '广告'
        },
        {
          value: 9,
          label: '导航'
        }

      ],
      sorting: {
        id: '',
        orders: ''
      },
      activeName:'9'
    }
  },
  components: {Pagination, SingleUpload},
  created() {
    this.loadTag()
  },
  filters: {
    watchType(val) {
      if (val == 0) {
        return '文章'
      } else if (val == 1) {
        return '客户'
      } else if (val == 2) {
        return '专题'
      } else if (val == 3) {
        return '商品'
      } else if (val == 4) {
        return '课程'
      } else if (val == 5) {
        return '量表'
      } else if (val == 6) {
        return '答主'
      } else if (val == 7) {
        return '咨询'
      } else if (val == 8) {
        return '广告'
      } else if (val == 9) {
        return '导航'
      }
    }
  },
  mounted() {
    this.comeIn()
    this.beforeMount()
  },
  methods: {
    // 表格排序
    sortChange(column) {
      console.log(column)
      this.sortField = column.prop;
      if (column.order == 'ascending') {
        this.direction = 'asc'
      } else {
        this.direction = 'desc'
      }
      this.loadBrand()
    },
    bulrOrders(data) {
      console.log(data)
      this.sorting.id = data.id;
      this.sorting.orders = data.orders;
      this.saveSorting();
    },
    saveSorting() {
      var _this = this
      update(_this.sorting).then(response => {
        console.log(response)
        _this.tagForm.forEach(function (item) {
          if (item.id == response.data.id) {
            item.orders = response.data.orders
          }
        });
        _this.$message({
          message: response.content,
          offset: 120,
          center: true
        });
      }).catch(error => {
        console.log(error)
      });
    },
    saveTag(addForm) {
      var _this = this;
      this.$refs[addForm].validate((valid) => {
        if (valid) { // 校验表单
          _this.clicked = true
          save(_this.addForm).then(response => {
            console.log(response);
            _this.clicked = false;
            _this.dialogFormVisible = false;
            _this.loadTag()
            _this.$message({
              message: response.content,
              offset: 120,
              center: true
            });
          }).catch(error => {
            _this.clicked = false;
            console.log('error!!')
          });
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
//新增
    addClick() {
      this.addForm.name = '';
      this.addForm.memo = '';
      this.addForm.type = '';
      this.addForm.icon = "";
      this.dialogFormVisible = true;
    },
    //  编辑
    editClick(row) {
      this.editForm.id = row.id;
      this.findTag()
      this.editVisible = true
    },
    findTag() {
      var _this = this;
      find(_this.editForm.id).then(response => {
        console.log(response)
        _this.editForm.name = response.data.tag.name;
        _this.editForm.memo = response.data.tag.memo;
        _this.editForm.icon = response.data.tag.icon;
        _this.editForm.type = JSON.stringify(response.data.tag.type);
        // _this.editForm.orders = repones.data.orders
      }).catch(error => {
        console.log('error!!')
      });
    },
    updateTag(editForm) {
      var _this = this
      this.$refs[editForm].validate((valid) => {
        if (valid) { // 校验表单
          _this.clicked = true
          update(_this.editForm).then(response => {
            console.log(response);
            _this.clicked = false;
            _this.editVisible = false;
            _this.loadTag()
            _this.$message({
              message: response.content,
              offset: 120,
              center: true
            });
          }).catch(error => {
            _this.clicked = false;
            console.log('error!!')
          });
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    beforeMount() {
      var _this = this
      window.onresize = () => {
        return (() => {
          if (this.queryType == false) {
            var h = document.documentElement.clientHeight || document.body.clientHeight;
            _this.curHeight = h - 266//减去页面上固定高度height
          } else {
            var h = document.documentElement.clientHeight || document.body.clientHeight;
            _this.curHeight = h - 220//减去页面上固定高度height
          }
        })();
      };
    },
    comeIn() {
      console.log('222')
      var h = document.documentElement.clientHeight || document.body.clientHeight;
      this.curHeight = h - 260//减去页面上固定高度height
    },
    chooseClick() {
      this.queryType = !this.queryType
      if (this.queryType == false) {
        var h = document.documentElement.clientHeight || document.body.clientHeight;
        this.curHeight = h - 266//减去页面上固定高度height
      } else {
        var h = document.documentElement.clientHeight || document.body.clientHeight;
        this.curHeight = h - 220//减去页面上固定高度height
      }
    },
    deleteTag(data) {
      console.log(data)
      var _this = this;
      this.$confirm('确认删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        del(data.id).then(response => {
          console.log(response)
          _this.tagForm.forEach(function (item, index) {
            if (item.id == data.id) {
              _this.tagForm.splice(index, 1)
            }
          });
          _this.$message({
            message: response.content,
            offset: 120,
            center: true
          });
        }).catch(error => {
          console.log('error!!')
        });
      }).catch(() => {

      })
    },
    queryTag() {
      this.keyword = this.query;
      this.pageNum = 1
      this.loadTag()
    },
    resetTag() {
      this.query.name = '';
      this.query.type = '';
      this.keyword = this.query;
      this.pageNum = 1;
      this.loadTag()
    },
    loadTag() {
      var _this = this
      let params = new URLSearchParams();
      params.append('pageNum', _this.pageNum);
      params.append('pageSize', _this.pageSize);
      params.append('name', _this.keyword.name);
      params.append('type', _this.keyword.type);
      list(params).then(response => {
        console.log(response)
        _this.tagForm = response.data.data
        _this.total = response.data.total
      }).catch(error => {
        console.log('error!!')
      });
    },
    handleClick(tab, event) {
      console.log(tab, event);
      this.pagenum = 0
      this.pageSize = 10
      this.keyword.name = ''
      if(tab.index == 0){
        this.keyword.type = 9
        this.loadTag()
      }else if(tab.index == 1){
        this.keyword.type = 3
        this.loadTag()
      }else if(tab.index == 2){
        this.keyword.type = 8
        this.loadTag()
      }else if(tab.index == 3){
        this.keyword.type = 0
        this.loadTag()
      }
    }
  }
}
</script>

<style scoped>
.el-footerBox {
  height: 50px !important;
  line-height: 50px !important;
  border-top: 1px solid #eeeeee;
}

/deep/ .el-dialog__header {
  border: 1px solid #eeeeee !important;
  padding: 15px !important;
}

/deep/ .el-dialog__body {
  padding: 0px 20px !important;
}

.popup_content {
  padding-bottom: 0px !important;
}

.app {
  background-color: rgba(248, 248, 248, 0.8);
}

/deep/ .el-header {
  padding: 10px;
  /* height: 100% !important; */
  padding-bottom: 0;
}

.report_header {
  width: 100%;
  min-height: 40px;
  box-sizing: border-box;
  background-color: rgba(248, 248, 248, 0.8);
  display: flex;
  align-items: center;
  padding-bottom: 5px;
  position: relative;
  padding-left: 5px;
  padding-right: 5px;
}

.report_header_left {
  width: 5px;
  height: 100%;
  background-color: #409EFF;
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
}

.report_header_date {
  margin-left: 10px;
  width: 250px;
  margin-top: 5px;
}

.report_header_query {
  margin-left: 10px;
  width: 180px;
  margin-top: 5px;
}
</style>
